package module.edittroop.view.editnewtrooppanel
{
	import code.analyze.ConfigAnalyze;
	import code.utils.MaterialUtil;
	
	import com.darwin.darwinUI.containers.ContainerBase;
	import com.darwin.darwinUI.controls.Button;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.TextInput;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.events.UIEvent;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.UIManager;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	
	import module.edittroop.event.EditTroopEvent;
	import module.edittroop.model.dataObj.ArmsConfigObj;
	import module.edittroop.model.dataObj.HeroConfigObj;
	import module.edittroop.view.troopinfopanel.EditTroopPanel;
	import module.edittroop.view.troopinfopanel.HeadFarem;
	import module.main.view.ResourceConst;

	/**
	 *编辑新部队大面板 
	 * @author jichang
	 * 
	 */	
	public class EditNewTroopPanel extends ContainerBase
	{
		private var _newTroopTotle:Sprite;
		private var _txtInputTroopNameBg:Sprite;
		/**
		 *大背景面板 
		 */		
		private var _editNewTroopBg:Sprite;
		/**
		 *确定按钮 
		 */		
		private var _sureBtn:Button;
		/**
		 *取消按钮 
		 */		
		private var _chanelBtn:Button;
		
		private var _closeBtn:Button;
		
		private var _titleLabel:Label;
		/**
		 *编辑部队输入 
		 */		
		private var _textInSp:Sprite;
		private var _textInSpTxt:Sprite;
		
		private var _attackNumTxt:Label;
		private var _attackSpeedNumTxt:Label;
		
		private var _heroInfoPanel:EditNewSoldierInfoPanel;
		
		private var _soldierInfoPanel:EditNewSoldierInfoPanel;
		
		private var uiComponent:UIComponent;
		private var _smallBg:Sprite
		public function EditNewTroopPanel()
		{
			_titleLabel = TextFieldFactory.getInstance().getLabel("create_title_2");
			_smallBg = new Sprite;
			_newTroopData = new Object;
			_editNewTroopBg = _editNewTroopBg = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_BACKGROUND) as Sprite;
			_editNewTroopBg.width = 465.8;
			_editNewTroopBg.height = 553.8;
			_smallBg.addChild(_editNewTroopBg);
			_smallBg.width = _editNewTroopBg.width;
			_smallBg.height = _editNewTroopBg.height;
			_heroInfoPanel = new EditNewSoldierInfoPanel(0);
			_soldierInfoPanel = new EditNewSoldierInfoPanel(1);
			_soldierInfoPanel.addEventListener(EditTroopEvent.ADD_TROOP_NUM,addHandler);
			_heroInfoPanel.list.addEventListener(UIEvent.LISTITEM_CLICK,uiClickHandler);
			_soldierInfoPanel.list.addEventListener(UIEvent.LISTITEM_CLICK,uiClickHandler);
			uiComponent = new UIComponent();
			this.addItem(uiComponent);
		}
		private function addHandler(e:EditTroopEvent):void
		{
			changNewTroopData(e.data.troopNum);
		}
		public function get titleLabel():Label
		{
			return _titleLabel;
		}

		public function set titleLabel(value:Label):void
		{
			_titleLabel = value;
		}

		public function get soldierInfoPanel():EditNewSoldierInfoPanel
		{
			return _soldierInfoPanel;
		}

		public function get txtNameInput():TextInput
		{
			return _txtNameInput;
		}

		/**
		 *设置输入新队伍名称 
		 * 
		 */
		public function set txtNameInput(value:TextInput):void
		{
			_txtNameInput = value;
		}

		private function initView():void
		{
			
			_sureBtn = new Button();
			_sureBtn.backgroundSkin = "EditTroop." + ResourceConst.EDIT_BLUE_BUTTON;
			
			_chanelBtn = new Button();
			_chanelBtn.backgroundSkin = "EditTroop." + ResourceConst.EDIT_TROOP_CHANNEL_BUTTON;
			_chanelBtn.backgroundAuto = true;
			
			_closeBtn = new Button();
			_closeBtn.backgroundSkin = "EditTroop." + ResourceConst.EDIT_TROOP_COLSE_BUTTON;
			_closeBtn.backgroundAuto = true;
			
			_textInSp = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_TEXTINPUT_PANEL) as Sprite;
			_txtInputTroopNameBg = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_NAME_INPUT) as Sprite;
			_newTroopTotle = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_TOTLE_INFO) as Sprite;
			_attackNumTxt = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			_attackSpeedNumTxt = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			
			setUiPosition();
			setRandomName();
		}
		private function setRandomName():void
		{
			
		}
		private function setUiPosition():void
		{
			uiComponent.addChild(_smallBg);
			setTextInSp();
			setHeroInfoPanel();
			setSoldierInfoPanel();
			setNewTroopTotle();
			setReorganizationBtn();
			//取消按钮
			setChanelBtn();
			
			setCloseBtn();
			
			setTxtInputTroopNameBg();
			
			setTxt();
		}
		private function setCloseBtn():void
		{
			_closeBtn.x = _editNewTroopBg.x + _editNewTroopBg.width - 42;
			_closeBtn.y = 13;
			_smallBg.addChild(_closeBtn);
			_closeBtn.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		private function setNewTroopTotle():void
		{
			_newTroopTotle.x = _heroInfoPanel.x ;
			_newTroopTotle.y = _soldierInfoPanel.y + 10 + _soldierInfoPanel.height;
			_smallBg.addChild(_newTroopTotle);
		}
		/**
		 *射程 
		 */		
		private var _rangeLabel:Label;
		private var _rangeNum:int = 0;
		/**
		 *暴击率 
		 */		
		private var _critRateLabel:Label;
		private var _critRateNum:Number = 0;
		/**
		 *防御力 
		 */		
		private var _defenceLabel:Label;
		private var _defenceNum:int;
		/**
		 *移动速度 
		 */		
		private var _speedLabel:Label;
		private var _speedNum:Number;
		private var _attackNum:int;
		private var _attackSpeedNum:int;
		private function setTxt():void
		{
			_attackNumTxt.text = "攻击 : ";
			_attackNumTxt.x = 20 ;
			_attackNumTxt.y = 15;
			_newTroopTotle.addChild(_attackNumTxt);
			
			_attackSpeedNumTxt.text = "攻击速度 : ";
			_attackSpeedNumTxt.x = _attackNumTxt.x;
			_attackSpeedNumTxt.y = _attackNumTxt.y + _attackNumTxt.height;
			_newTroopTotle.addChild(_attackSpeedNumTxt);
			
			_rangeLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			_rangeLabel.text = "射程 : ";
			_rangeLabel.x = _newTroopTotle.width / 2 - _rangeLabel.width + 5;
			_rangeLabel.y = _attackNumTxt.y;
			_newTroopTotle.addChild(_rangeLabel);
			
			_critRateLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			_critRateLabel.text = "暴击率 : ";
			_critRateLabel.x = _rangeLabel.x;
			_critRateLabel.y = _attackSpeedNumTxt.y;
			_newTroopTotle.addChild(_critRateLabel);
			
			_defenceLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			_defenceLabel.text = "防御 : ";
			_defenceLabel.x = _rangeLabel.x + _rangeLabel.x - _attackNumTxt.x;
			_defenceLabel.y = _attackNumTxt.y;
			_newTroopTotle.addChild(_defenceLabel);
			
			_speedLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			_speedLabel.text = "移动速度 : ";
			_speedLabel.x = _defenceLabel.x;
			_speedLabel.y = _critRateLabel.y;
			_newTroopTotle.addChild(_speedLabel);
		}
		private var _txtNameInput:TextInput;
		private function setTxtInputTroopNameBg():void
		{
			_txtNameInput = new TextInput();
			_txtNameInput.width = _txtInputTroopNameBg.width;
			_txtNameInput.height = _txtInputTroopNameBg.height;
			_txtInputTroopNameBg.x = _heroInfoPanel.x + 10;
			_txtInputTroopNameBg.y = 20;
			_txtNameInput.border = false;
			_txtNameInput.x = (_txtInputTroopNameBg.width - _txtNameInput.width) / 2;
			_smallBg.addChild(_txtInputTroopNameBg);
			_txtInputTroopNameBg.addChild(_txtNameInput);
		}
		/**
		 *士兵信息面板设置 
		 * 
		 */		
		private function setSoldierInfoPanel():void
		{
			_soldierInfoPanel.x = _heroInfoPanel.x;
			_soldierInfoPanel.y = _heroInfoPanel.y + _heroInfoPanel.height + 10;
			_soldierInfoPanel.list.source = _armsConfigData;
			_smallBg.addChild(_soldierInfoPanel);
		}
		private function setHeroInfoPanel():void
		{
			_heroInfoPanel.x = (_smallBg.width - _heroInfoPanel.width) / 2;
			_heroInfoPanel.y = 50;
			_heroInfoPanel.list.source = _heroConfigData;
			_smallBg.addChild(_heroInfoPanel);
		}
		private function setTextInSp():void
		{
			_textInSp.x = _smallBg.x + (_smallBg.width - _textInSp.width) / 2;
			_textInSp.y = _smallBg.y - _textInSp.height / 2 + 5;
			_titleLabel.x = (_textInSp.width - _titleLabel.width) / 2 ;
			_titleLabel.y =  (_textInSp.height - _titleLabel.height) / 2;
			_smallBg.addChild(_textInSp);
			_textInSp.addChild(_titleLabel);
		}
		private function setReorganizationBtn():void
		{
			var $label:Label = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			$label.text = "确定";
			_sureBtn.width = 82.0;
			_sureBtn.height = 34.0;
			_sureBtn.x = (_smallBg.width - _sureBtn.width) / 2 - _sureBtn.width;
			_sureBtn.y = (_smallBg.height - _soldierInfoPanel.y - _soldierInfoPanel.height) / 2 +_soldierInfoPanel.y + _soldierInfoPanel.height;
			$label.x = (_sureBtn.width - $label.width) / 2;
			$label.y = (_sureBtn.height - $label.height) / 2;
			_smallBg.addChild(_sureBtn);
			_sureBtn.addChild($label);
			_sureBtn.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		private function setChanelBtn():void
		{
			_chanelBtn.x =  (_smallBg.width - _sureBtn.width) / 2 + _sureBtn.width;
			_chanelBtn.y = _sureBtn.y;
			_chanelBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			_smallBg.addChild(_chanelBtn);
		}		
		private function clickHandler(e:MouseEvent):void
		{
			switch(e.currentTarget)
			{
				case _chanelBtn:
					var removeNewTroopPanel:EditTroopEvent = new EditTroopEvent(EditTroopEvent.REMOVE_NEW_TROOP_PANEL);
					dispatchEvent(removeNewTroopPanel);
					break;
				case _sureBtn:
						var $editEvent:EditTroopEvent = new EditTroopEvent(EditTroopEvent.EDIT_NEW_TROOP);
						if(_newTroopData.heroId&&_newTroopData.armsId&&_soldierInfoPanel.troopNum !=0)
						{
							var $choosePanelEvent:EditTroopEvent = new EditTroopEvent(EditTroopEvent.EDIT_DISSLOVE)
							this.dispatchEvent($choosePanelEvent);
							_newTroopData.needNum = _soldierInfoPanel.troopNum;
							_newTroopData.troop_name = _txtNameInput.text;
							$editEvent.data = _newTroopData;
							dispatchEvent($editEvent);
						}
						else
							return;
					break;
				case _closeBtn:
					UIManager.getPopUpManager().removePopUp(this);
				default:
					break;
			}
		}
		/**
		 *更新数据 
		 * 
		 */	
		private var _newTroopData:Object;
		public function changNumData(data:Object,num:int):void
		{
			if(ishero)
			{
				_newTroopData.heroId = (data as HeroConfigObj).serverId;
			}
			else
			{
				_newTroopData.armsId = data.serverId;
				_attackNum = data.atk;
				_attackSpeedNum = data.atkRate;
				_rangeNum = data.range;
				_defenceNum = data.def;
				_critRateNum = data.crit;
				_speedNum = data.speed;
				changNewTroopData(1);
			}
		}
		private var ishero:Boolean = false;
		private function uiClickHandler(e:UIEvent):void
		{
			switch(e.currentTarget)
			{
				case _heroInfoPanel.list:
					if((e.data as HeadFarem) !=_headFaremFitel&&_headFaremFitel)
					{
						removeFilter();
					}
					_headFaremFitel = e.data as HeadFarem;
					var $fiter:GlowFilter = new GlowFilter();
					$fiter.alpha = 0.5
					var $filtersArray:Array = new Array($fiter); 
					(e.data as HeadFarem).filters = $filtersArray;
					ishero = true;
					changNumData((e.data as HeadFarem).data,1);
					_heroInfoPanel.changInfoData((e.data as HeadFarem).data);
					_soldierInfoPanel.heroInfo = (e.data as HeadFarem).data;
					break;
				case _soldierInfoPanel.list:
					if((e.data as HeadFarem) !=_soldierheadFaremFitel&&_soldierheadFaremFitel)
					{
						removeSoldierFilter();
					}
					_soldierheadFaremFitel = e.data as HeadFarem;
					var $fiter_1:GlowFilter = new GlowFilter();
					$fiter_1.alpha = 0.5
					var $filtersArray_1:Array = new Array($fiter_1); 
					(e.data as HeadFarem).filters = $filtersArray_1;
					ishero = false;
					changNumData((e.data as HeadFarem).data,1);
					_soldierInfoPanel.changInfoData((e.data as HeadFarem).data);
					break;
			}
		}
		private function removeSoldierFilter():void
		{
			_soldierheadFaremFitel.filters = [];
		}
		private function removeFilter():void
		{
			_headFaremFitel.filters = [];
		}
		private var _soldierheadFaremFitel:HeadFarem;
		private var _headFaremFitel:HeadFarem;
		private function changNewTroopData(num:int):void
		{
			_attackNumTxt.text = "攻击 : " + (_attackNum*num).toString();
			_attackSpeedNumTxt.text = "攻击速度 : " + (_attackSpeedNum*num).toString();
			_rangeLabel.text = "射程 : " + (_rangeNum * num).toString();
			_defenceLabel.text = "防御 : " + (num * _defenceNum).toString();
			_critRateLabel.text = "暴击率 : " +(_critRateNum * num).toString();
			_speedLabel.text = "移动速度 : " + (_speedNum * num).toString();
		}
		private var _heroConfigData:Array;
		private var _armsConfigData:Array;
		public function setData(heroArr:Array,armsArr:Array):void
		{
			_heroConfigData = heroArr;
			_armsConfigData = armsArr;
			initView();
			this.height = 553.8 + _textInSp.height / 2 ;
			this.width = 465.8;
			uiComponent.y = 20;
		}
	}
}